﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.HVAC;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayWaterPump : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
				ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_MechanicalEquipment);
				ILayForm layForm = new ILayForm(document);
				FamilySymbol familySymbol = layForm.LayWaterPumpFormShow(document, "水泵");
				EquipmentParam equipmentPara = layForm.equipmentPara;
				if (familySymbol == null)
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					if (familySymbol != null)
					{
						try
						{
							activeUIDocument.PromptForFamilyInstancePlacement(familySymbol);
						}
						catch (Exception)
						{
						}
					}
					ICollection<ElementId> allFamilyInstance2 = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_MechanicalEquipment);
					ICollection<Element> allTag = hyequipmentCommon.GetAllTag(document, BuiltInCategory.OST_MechanicalEquipmentTags);
					transaction = new Transaction(document, "设置水泵的参数");
					transaction.Start();
					Element element = null;
					Element element2 = null;
					bool flag = true;
					foreach (ElementId elementId in allFamilyInstance2)
					{
						bool flag2 = false;
						foreach (ElementId elementId2 in allFamilyInstance)
						{
							if (elementId == elementId2)
							{
								flag2 = true;
								break;
							}
						}
						if (!flag2)
						{
							element = document.GetElementById(elementId);
							bool flag3 = false;
							foreach (Element element3 in allTag)
							{
								if (((IndependentTag)element3).TaggedLocalElementId.IntegerValue == elementId.IntegerValue)
								{
									flag3 = true;
									element2 = element3;
									allTag.Remove(element3);
									break;
								}
							}
							if (!flag3 && equipmentPara.Tag && flag)
							{
								try
								{
									RevitVersionFuncs.NewTag(document, document.ActiveView, new Reference(element), false, 0, 0, (element.Location as LocationPoint).Point);
									goto IL_1DB;
								}
								catch (Exception)
								{
									flag = false;
									MessageBox.Show("标注失败，可能原因是项目里没有标记族，请手动加载并配置", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
									goto IL_1DB;
								}
								goto IL_1C0;
							}
							goto IL_1C0;
							IL_1DB:
							Parameter parameter = null;
							Parameter parameter2 = null;
							double num = 0.0;
							if (element.GetParameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM) != null)
							{
								parameter2 = element.GetParameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM);
							}
							else
							{
								Parameter parameter3 = element.GetParameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
								if (parameter3 != null)
								{
									parameter3.Set(document.ActiveView.GenLevel.Id);
								}
							}
							if (parameter2 != null)
							{
								parameter2.Set(UnitConvert.CovertToAPI(equipmentPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
								num = parameter2.AsDouble();
							}
							if (num != UnitConvert.CovertToAPI(equipmentPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2))
							{
								if (element.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM) != null)
								{
									parameter = element.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
								}
								if (parameter != null)
								{
									parameter.Set(UnitConvert.CovertToAPI(equipmentPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
								}
							}
							document.Regenerate();
							continue;
							IL_1C0:
							if (flag3 && !equipmentPara.Tag)
							{
								document.Delete(element2.Id);
								goto IL_1DB;
							}
							goto IL_1DB;
						}
					}
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
